package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.*;
import com.xjh.t.domain.Permission;
import com.xjh.t.domain.Role;
import com.xjh.t.service.IPermissionService;
import com.xjh.t.service.IRoleService;
import com.xjh.t.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-28
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    
    @RequestMapping("loadAllRole")
    public DataGridView loadAllRole(RoleVo vo)
    {
        IPage<Role> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vo.getName()),"name",vo.getName());
        queryWrapper.like(StringUtils.isNotBlank(vo.getRemark()),"remark",vo.getRemark());
        queryWrapper.eq(vo.getAvailable() != null,"available",vo.getAvailable());

        queryWrapper.orderByDesc("createtime");
        this.roleService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    @RequestMapping("addRole")
    public ResultObj addRole(RoleVo vo)
    {
        try {
            vo.setCreatetime(new Date());
            this.roleService.save(vo);
            return ResultObj.ADD_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("updateRole")
    public ResultObj updateRole(RoleVo vo)
    {
        try {

            this.roleService.updateById(vo);
            return ResultObj.ADD_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("deleteRole")
    public ResultObj deleteRole(Integer id)
    {
        try {

            this.roleService.removeById(id);
            return ResultObj.DELETE_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("initRolePermissionByRoleId")
    public DataGridView initRolePermissionByRoleId(Integer roleId)
    {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available", Constast.TYPE_AVAILABLE_TRUE);
        List<Permission> allPermission = this.permissionService.list(queryWrapper);

        List<Integer> currentRolePermissions = this.roleService.queryRolePermissionIdsByRid(roleId);

        List<Permission> currentPermissions = new ArrayList<>();
        if(currentRolePermissions.size() > 0) {
            queryWrapper.in("id", currentRolePermissions);
           currentPermissions = this.permissionService.list(queryWrapper);
        }

        List<TreeNode> nodes = new ArrayList<>();
        for(Permission p1:allPermission)
        {
            String checkArr = "0";
            for(Permission p2:currentPermissions)
            {
                if(p1.getId() == p2.getId())
                {
                    checkArr = "1";
                    break;
                }
            }

            TreeNode node = new TreeNode(p1.getId(),p1.getPid(),p1.getTitle(),p1.getOpen() == null || p1.getOpen() == Constast.OPEN_TRUE,checkArr);
            nodes.add(node);
        }

        return new DataGridView(nodes);
    }

    @RequestMapping("saveRolePermission")
    public ResultObj saveRolePermission(Integer id,Integer[] ids)
    {
        try
        {
            this.roleService.saveRolePermission(id,ids);
            return ResultObj.DISPATCH_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DISPATCH_ERROR;
        }
    }
    
}

